package com.yunmang.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Handler;
import android.os.Looper;

import com.bigkoo.alertview.AlertView;
import com.bigkoo.alertview.OnItemClickListener;
import com.yunmang.R;
import com.yunmang.app.AppConstant;
import com.yunmang.entity.PackageBean;
import com.yunmang.http.HttpUtils;
import com.yunmang.utils.DeviceUtils;
import com.yunmang.utils.SharedPreferencesUtils;

import java.io.IOException;
import java.io.StreamCorruptedException;
import java.util.List;

import rx.Subscriber;

/**
 * Created by Administrator on 2017/5/6.
 */
public class SplashActivity extends BaseActivity implements OnItemClickListener {
    Handler mHandler;
    private Subscriber<PackageBean> subscriber;
    private int i=3;
    @Override
    public int getLayoutId() {
        return R.layout.activity_splash;
    }

    @Override
    public void initView() {
        if(!DeviceUtils.isHasNetWork(this)){//请检查网络设置
            new AlertView("网络异常", "网络不可用，请检查您的网络环境设置", null, new String[]{"确定"}, null, this, AlertView.Style.Alert, this).show();
            return;
        }
        mHandler = new Handler(Looper.getMainLooper());
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                getCommonOption();
            }
        },1000);
    }

    @Override
    public void initData() {

    }

    private void getCommonOption(){
        i--;
        subscriber=new Subscriber<PackageBean>() {
            @Override
            public void onCompleted() {

            }
            @Override
            public void onError(Throwable e) {
                if(i>0){
                    getCommonOption();
                }else{
                    showToast("连接服务器超时，请退出重新进入");
                }
            }
            @Override
            public void onNext(PackageBean packageBean) {
                if(packageBean.getCode()==200){
                    AppConstant.balanceMethodList=packageBean.getModel().getBalanceMethod();
                    AppConstant.packList= packageBean.getModel().getPack();
                    saveBalanceList(packageBean.getModel().getBalanceMethod());
                    startActivity(new Intent(context,LoginActivity.class));
                    finish();
                }
            }
        };
        HttpUtils.getInstance().getAllPackages(subscriber);
    }

    @Override
    public void onItemClick(Object o, int position) {
        if(position!=AlertView.CANCELPOSITION){
            finish();
        }
    }

    // 保存list
    private void saveBalanceList(List<PackageBean.ModelBean.BalanceMethodBean> bean) {
        SharedPreferences mySharedPreferences = getSharedPreferences(AppConstant.balance_file, Context.MODE_PRIVATE);
        SharedPreferences.Editor edit = mySharedPreferences.edit();
        try {
            String liststr = SharedPreferencesUtils.SceneList2String(bean);
            edit.putString("orderlist", liststr);
            edit.commit();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // 保存list
    private void savePackList(List<PackageBean.ModelBean.PackBean> bean) {
        SharedPreferences mySharedPreferences = getSharedPreferences(AppConstant.pack_file, Context.MODE_PRIVATE);
        SharedPreferences.Editor edit = mySharedPreferences.edit();
        try {
            String packlist = SharedPreferencesUtils.SceneList2String(bean);
            edit.putString("packlist", packlist);
            edit.commit();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


}
